package com.example.tools.common.login;

import com.example.tools.common.base.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 登录
 * Created by Administrator on 2019-09-15.
 */
@Controller
public class LoginController {

    @Autowired
    private UserService usi;

    @RequestMapping("/login")
    public String login(Map<String,Object> map){
        return "login/login";
    }

    @RequestMapping("/main.page")
    public String toMain(Map<String,Object> map,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("userInfo");
        if(user==null){
            return "/login";
        }
        map.put("userInfo",request.getSession().getAttribute("userInfo"));
        return "/main/main";
    }

    /**
     * 登录用户
     * @param user
     * @return
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    public Object doLogin(User user,HttpServletRequest request){
//        System.out.printf(user.toString());
        CommonResult cr =  usi.checkUser(user.getLoginName(),user.getPassword());
        //
        request.getSession().setAttribute("userInfo", user);
        return cr;
    }


    /**
     * 登录页面
     * @param map
     * @param user
     * @return
     */
    @RequestMapping("/webLogin")
    public String webLogin(Map<String,Object> map, User user) {
        System.out.printf(user.toString());
        CommonResult cr = usi.checkUser(user.getLoginName(), user.getPassword());
        if (cr.getCode() == 100) {
            //登陆成功
            map.put("userInfo", user);
            return "main/main";
        } else {
            return "redirect:login/login";
        }
    }
}
